package com.custom.base.util

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import com.custom.base.R
import com.custom.base.base.AnimationActivity
import com.custom.base.manager.SDActivityManager
import com.custom.base.type.AnimationActivityType

object IntentUtil {
    /**
     * 页面跳转
     * @param clz
     * @param bundle
     * @param requestCode
     * @param flags
     * @param view
     * @param activity
     * @param animationStart 启动动画
     * @param animationEnd 关闭动画
     */
    fun startActivity(
        clz: Class<*>,
        bundle: Bundle? = null,
        requestCode: Int = 0,
        flags: List<Int> = listOf(),
        view: View? = null,
        activity:Activity? = null,
        animationStartType: AnimationActivityType = AnimationActivityType.BOTTOM,
        animationEndType:Int = R.anim.animo_no
    ) {
        val mActivity:Activity = (activity ?: SDActivityManager.instance.lastActivity) as Activity
        val intent = Intent(mActivity, clz)
        for (flag in flags) {
            intent.addFlags(flag)
        }
        bundle?.apply {
            intent.putExtras(this)
        }

        if(requestCode == 0){
            if(view == null) mActivity.startActivity(intent)
            else view.setOnClickListener {
                mActivity.startActivity(intent)
            }
        }else{
            if(view == null) mActivity.startActivityForResult(intent,requestCode)
            else view.setOnClickListener {
                mActivity.startActivityForResult(intent,requestCode)
            }
        }
        (mActivity as AnimationActivity).startAnimation(animationStartType,animationEndType)
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_DIAL)
        val data: Uri = Uri.parse("tel:$phoneNum")
        intent.data = data
        SDActivityManager.instance.lastActivity?.startActivity(intent)
    }
}